package com.thghh.graph;

/**
 * 顶点
 *
 * @author pengzhikang
 * @date 2024/8/19 15:28
 * @since 1.0.0
 */
public class Vertex {
    /** 名称 */
    private String name;

    /** 边缘 */
    private Edge edge;

    /** 边数 */
    private int numOfEdges;

    private boolean visited;

    public Vertex(String name) {
        this.name = name;
    }

    public Vertex(String name, Edge edge) {
        this.name = name;
        this.edge = edge;
    }

    /**
     * 添加边
     *
     * @param edge 边缘
     * @author pengzhikang
     * @date 2024/08/19 15:53
     */
    public void addEdge(Edge edge) {
        // 新建边
        if (this.edge == null) {
            // 不存在边，则直接插入
            this.edge = edge;
        } else {
            // 如果已经存在边，则将新的边添加到链表的最后
            Edge lastEdge = this.edge;
            while (lastEdge.getNext() != null) {
                lastEdge = lastEdge.getNext();
            }
            lastEdge.setNext(edge);
        }

        // 边的个数加一
        this.numOfEdges++;
    }

    /**
     * 根据对应的顶点名称获取对应的边
     *
     * @param name 名称
     * @return {@link Edge }
     * @author pengzhikang
     * @date 2024/08/19 16:00
     */
    public Edge getEdge(String name) {
        Edge edge = this.edge;
        while (!edge.getName().equals(name)) {
            edge = edge.getNext();
        }
        return edge;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Edge getEdge() {
        return edge;
    }

    public void setEdge(Edge edge) {
        this.edge = edge;
    }

    public int getNumOfEdges() {
        return numOfEdges;
    }

    public void setNumOfEdges(int numOfEdges) {
        this.numOfEdges = numOfEdges;
    }

    public boolean isVisited() {
        return visited;
    }

    public void setVisited(boolean visited) {
        this.visited = visited;
    }
}
